//
//  FloatCanvas.m
//  AifudaoDrawer
//
//  Created by Ji Hu on 12-5-16.
//  Copyright (c) 2012年 爱辅导. All rights reserved.
//

#import "FloatCanvas.h"
#import "Log.h"
#import "DCGlobal.h"

@implementation FloatCanvas

@synthesize mPath;
@synthesize mDispatcher;
//@synthesize mGestureRecognizer;
//@synthesize mScroller;
//@synthesize sender;
@synthesize from;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        [self setOpaque:NO];
        self.mPath = [UIBezierPath bezierPath];
//        self.multipleTouchEnabled=TRUE;
//        UIPanGestureRecognizer *gr=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScrollGesture:)];
//        
//        self.mGestureRecognizer=gr;
//        
//        [gr release];
//        
//        [mGestureRecognizer setMinimumNumberOfTouches:2];
//        [mGestureRecognizer setMaximumNumberOfTouches:3];
//        [self addGestureRecognizer:mGestureRecognizer];
        
        TouchOffset=0;
        
//        from = FROM_DRAWER;
    }
    return self;
}

-(void)touchUpAndDispatch{
    
    if ([mDispatcher respondsToSelector:@selector(dispatchPathOffset)]) {
        CGPoint offpoint = [mDispatcher dispatchPathOffset];
        
        
        CGFloat dx=offpoint.x;
        CGFloat dy=offpoint.y;
        
        [mPath applyTransform:CGAffineTransformMakeTranslation(dx, dy)];
    }
    
    [mDispatcher dispatchPath:mPath withColor:[DCGlobal localColor]];
    [self.mPath removeAllPoints];
    [self setNeedsDisplay];
}


//-(void)handleScrollGesture:(UIPanGestureRecognizer *)gestureRecognizer{
//    
////    CGPoint point=[gestureRecognizer locationInView:self];
//    if([gestureRecognizer numberOfTouches]<=1) return;
//    
//    CGPoint point = [gestureRecognizer locationOfTouch:0 inView:self];
//      
//    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
////        NSLog(@"gesture begin.");  
//        [self.mScroller scroll:&point state:UIGestureRecognizerStateBegan];
//    }
//    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
//         NSLog(@"gesture end.");  
//    }else if ( gestureRecognizer.state == UIGestureRecognizerStateCancelled){
//        NSLog(@"gesture cancel.");  
//    }
//    else if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized){
//        NSLog(@"gesture recognized.");  
//    }
//    else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
////         NSLog(@"gesture changed.");  
//        
//        [self.mScroller scroll:&point state:UIGestureRecognizerStateChanged];
//    }
//
//}

static struct CGRect outRect(CGRect rect,int offset){
    return CGRectMake((rect).origin.x-offset,(rect).origin.y-offset,(rect).size.width+2*offset,(rect).size.height+2*offset);
}

-(void)drawRect:(CGRect)rect{
    
//    NSLog(@"draw float rect");
    CGContextRef context=UIGraphicsGetCurrentContext();
    
//    CGContextBeginPath(context);
    CGContextAddPath(context, self.mPath.CGPath);
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [DCGlobal localColor].CGColor);
    
//    CGSize cs = {1, 1};
//    CGContextSetShadow(context, cs, 0.5);

    CGContextDrawPath(context, kCGPathStroke);
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
//     NSLog(@"began count %d",[event allTouches].count);
    
    if ([event allTouches].count>1) {
        return;
    }
    
    UITouch *touch=[touches anyObject];
    
    CGPoint temp = [touch locationInView:self];
//    NSLog(@"touch begin: x-%f y-%f",temp.x,temp.y);
    CGPoint start = CGPointMake(temp.x, temp.y-TouchOffset);
    
    [mPath moveToPoint:start];
    
    mLastPoint=start;
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if ([event allTouches].count>1) {
        return;
    }
    
    UITouch *touch=[touches anyObject];
    
    CGPoint temp = [touch locationInView:self];

    CGPoint to = CGPointMake(temp.x, temp.y-TouchOffset);
    
    float dx=fabs(to.x-mLastPoint.x);
    float dy=fabs(to.y-mLastPoint.y);
    
    if (dx > 3 || dy >3) {
        
//        [self buildSyncDrawLines:to];
        [self.mPath addQuadCurveToPoint:CGPointMake((to.x+mLastPoint.x)/2, (to.y+mLastPoint.y)/2) controlPoint:(mLastPoint)];
        mLastPoint=to;
        CGRect dirty=(self.mPath.bounds);
        [self setNeedsDisplayInRect:outRect(dirty,2)];
        
    }

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch=[touches anyObject];
    CGPoint temp = [touch locationInView:self];
    CGPoint end = CGPointMake(temp.x, temp.y-TouchOffset);
    [mPath addLineToPoint:end];
    
    [self touchUpAndDispatch];
}

-(void)reset{
    
    if ([mPath isEmpty]==NO) {
        [self touchUpAndDispatch];
    }
    
}

-(void)dealloc{
//    [self removeGestureRecognizer:mGestureRecognizer];
//    [mGestureRecognizer release];
    [mPath release];
    [super dealloc];
}

@end
